ostree-soft-reboot: fix sysroot.mount umount
authorEtienne Champetier <e.champetier@ateme.com>
Wed, 3 Sep 2025 18:12:27 +0000 (14:12 -0400)
committerEtienne Champetier <e.champetier@ateme.com>
Thu, 4 Sep 2025 19:04:39 +0000 (15:04 -0400)
The composefs at /run/nextboot uses /sysroot, so systemd fails to
umount sysroot.mount during soft-reboot.
Create a temporary bind-mount, use it to prepare /run/nextboot
and MNT_DETACH it when we are done.

src/libostree/ostree-soft-reboot.c

index 809b79914607dfd98f40ad5e9ec7c8be4c0eb04f..a015cb4db008a0a7e791cd06b4ab5c6cc0e4cb94 100644 (file)
@@ -63,6 +63,22 @@ _ostree_prepare_soft_reboot (GError **error)
   if (!glnx_shutil_mkdir_p_at (AT_FDCWD, OTCORE_RUN_NEXTROOT, 0755, NULL, error))
     return FALSE;
 
+  /* Bind-mount /sysroot on itself.
+   * The composefs mount at /run/nextboot is going to use /sysroot,
+   * causing systemd to fail to umount sysroot.mount during soft-reboot.
+   * Create a temporary bind-mount, and MNT_DETACH it when we are done
+   */
+  if (mount (sysroot_path, sysroot_path, NULL, MS_BIND | MS_SILENT, NULL) < 0)
+    err (EXIT_FAILURE, "failed to MS_BIND '%s'", sysroot_path);
+
+  /* Our curent working directory is in the old /sysroot,
+   * ie we are under the bind mount, so run 'cd $PWD'
+   * to move to the new /sysroot
+   */
+  g_autofree char *cwd = g_get_current_dir ();
+  if (chdir (cwd) < 0)
+    err (EXIT_FAILURE, "failed to chdir to '%s'", cwd);
+
   // Tracks if we did successfully enable it at runtime
   bool using_composefs = false;
   if (!otcore_mount_rootfs (rootfs_config, &metadata_builder, sysroot_path, target_deployment,
@@ -78,6 +94,14 @@ _ostree_prepare_soft_reboot (GError **error)
   if (!otcore_mount_etc (config, &metadata_builder, OTCORE_RUN_NEXTROOT, error))
     return FALSE;
 
+  // detach the temporary /sysroot bind-mount
+  if (umount2 (sysroot_path, MNT_DETACH) < 0)
+    err (EXIT_FAILURE, "failed to MS_DETACH '%s'", sysroot_path);
+
+  // run 'cd $PWD' again to go back to the old /sysroot
+  if (chdir (cwd) < 0)
+    err (EXIT_FAILURE, "failed to chdir to '%s'", cwd);
+
   // And set up /sysroot. Here since we hardcode composefs, we also hardcode
   // having a read-only /sysroot.
   g_variant_builder_add (&metadata_builder, "{sv}", OTCORE_RUN_BOOTED_KEY_SYSROOT_RO,